#include "form_status_bar.h"
#include "ui_form_status_bar.h"

#include "utils/configure/configure_bll.h"

#ifdef QT_NO_DEBUG
#define DF_U_STORE_SS   "/udisk/ripnano_export"
#else
#define DF_U_STORE_SS   "/home/twdz/ripnano_export"
#endif

// add necessary includes here
#include <QScreen>

/*
 * Function:    run
 * Description: Default thread function
 * Paramer:     void
 * Return:      void
 */
void ScreenshotThread::run()
{
    qDebug() << Q_FUNC_INFO;

    QFile fStoreDev(DF_U_STORE_SS);
    if(fStoreDev.exists()) {
        QString sCurTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss");
        QString sImgName = QString("%1/%2.png").arg(DF_U_STORE_SS).arg(sCurTime);
        QString strCMD = QString("gsnap %1 /dev/fb0").arg(sImgName);

        int nRetVal = ::system(strCMD.toLatin1().data());

        ::system(QString("sync").toLatin1().data());
        qDebug() << Q_FUNC_INFO << nRetVal << strCMD;
    }
}

/*
 * Function:    FormStatusBar
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormStatusBar::FormStatusBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormStatusBar)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * Function:    ~FormStatusBar
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormStatusBar::~FormStatusBar()
{
    qDebug() << Q_FUNC_INFO;

    if(m_pTimerNow) {
        if(m_pTimerNow->isActive()) {
            m_pTimerNow->stop();
        }
    }

    if(m_pScreenshotThread) {
        m_pScreenshotThread->wait(1000);
        m_pScreenshotThread->quit();
    }

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormStatusBar" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "WidgetManager" class
 */
FormStatusBar* FormStatusBar::getInstance(QWidget *pParent)
{
    qDebug() << Q_FUNC_INFO;

    static FormStatusBar *pNew = NULL;
    if(!pNew) {
        // Create a new object if it doesn't exist
        pNew = new FormStatusBar(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormStatusBar::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    if(!m_pTimerNow) m_pTimerNow = new QTimer();
    Q_ASSERT(NULL != m_pTimerNow);

//    if(!m_pScreenshotThread) m_pScreenshotThread = new ScreenshotThread(this);
//    Q_ASSERT(NULL != m_pScreenshotThread);

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(m_pTimerNow, SIGNAL(timeout()), this, SLOT(slotShowCurrentTime()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }

    m_pTimerNow->setInterval(DEF_SEC_INTERVAL);
    m_pTimerNow->start();
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormStatusBar::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/home/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->label_SystemMode->setText("");
    ui->label_Net->setVisible(true);
    ui->label_Usb->setVisible(true);
    ui->label_DateTime->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    ui->label_Net->setVisible(false);
    ui->label_Usb->setVisible(false);

    // Display product name
    _showProductName();
}

/*
* Function:		on_pushButton_SystemMode_clicked
* Description:	Exec screenshot after click the button
* Paramer:		void
* Return:		void
*/
void FormStatusBar::on_pushButton_SystemVersion_clicked()
{
    qDebug() << Q_FUNC_INFO;

    // Get the login mode
    ST_CFG_LOGIN_MODE stLoginMode;
    ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

    if(E_MODE_FACTORY != stLoginMode.eumLoginMode) return;

#ifdef QT_NO_DEBUG
    // Prevent the button from clicking repeatedly quickly
    ui->pushButton_SystemVersion->setEnabled(false);
    QTimer::singleShot(3000, this, [=]() {
        ui->pushButton_SystemVersion->setEnabled(true);
    });

    // Start screenshot after the thread run
    m_pScreenshotThread->start();
#else
    QTimer::singleShot(1000, this, [=](){
        QScreen* pScreen = QGuiApplication::primaryScreen();
        QPixmap objPxmp = pScreen->grabWindow(0).copy(0, 0, DEF_MAINFRAME_WIDTH, DEF_MAINFRAME_HEIGHT);
        QImage objImg = objPxmp.toImage();
        QString sCurTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss");
        QString sImgName = QString("%1/%2.png").arg(DF_U_STORE_SS).arg(sCurTime);
        objImg.save(sImgName, "png");
    });
#endif
}

/*
* Function:		slotShowCurrentTime
* Description:	Display the current time
* Paramer:		void
* Return:		void
*/
void FormStatusBar::slotShowCurrentTime()
{
    //qDebug() << Q_FUNC_INFO;

    QString sNow = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    ui->label_DateTime->setText(sNow);
}

/*
* Function:		slotRefreshLoginMode
* Description:	Refresh login mode on the status bar
* Paramer:		void
* Return:		void
*/
void FormStatusBar::slotRefreshLoginMode(int eumLoginMode)
{
    qDebug() << Q_FUNC_INFO << eumLoginMode;

    switch ((int)eumLoginMode) {
    case E_MODE_GUEST:
        ui->label_SystemMode->setText(tr("User Mode"));
        break;
    case E_MODE_ADMIN:
        ui->label_SystemMode->setText(tr("Admin Mode"));
        break;
    case E_MODE_FACTORY:
        ui->label_SystemMode->setText(tr("Factory Mode"));
        break;
    default:
        ui->label_SystemMode->setText(tr("User Mode"));
        break;
    }
}

/*
 * Function:    _loadProductName
 * Description: Display product name
 * Paramer:     void
 * Return:      void
 */
void FormStatusBar::_showProductName()
{
    qDebug() << Q_FUNC_INFO;

    QString sProductName = QString("%1").arg(DEF_DEVICE_MODEL);
    ui->pushButton_SystemVersion->setText(sProductName);
}
